<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserCreateRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Http\Resources\RoleResource;
use App\Http\Resources\UserResource;
use App\Models\Role;
use App\Models\User;
use Faker\Provider\Uuid;
use Illuminate\Http\Request;

class UserController extends Controller
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new User();
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $perPage  = $request->get('limit', config('paginate.per_page'));
        $pageName = config('paginate.page_name');
        $page     = $request->get($pageName, 1);
        $rows     = $this->model::with('roles')->orderByDesc('updated_at')->paginate($perPage, ['*'], $pageName, $page);
        return $this->success('get data success', UserResource::collection($rows)->hide(['avatar', 'information']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param UserCreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(UserCreateRequest $request)
    {
        $data = $request->except(['roles']);
        $data['uuid'] = Uuid::uuid();
        $data['status'] = 1;
        $data['password'] = bcrypt($data['password']);
        $user = $this->model::create($data);
        if (!$user) {
            return $this->failure('failed to create user');
        }
        $roles = $request->only(['roles']);
        $user->syncRoles($roles);
        return $this->success('create success', $user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UserUpdateRequest $request
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(UserUpdateRequest $request, $id)
    {
        $user = $this->model::find($id);
        if (!$user) {
            return $this->failure("failed to get user#{$id}");
        }
        $data = $request->except(['password', 'roles']);
        if ($request->get('password')){
            $data['password'] = bcrypt($request->get('password'));
        }
        $res = $user->update($data);
        if (!$res) {
            return $this->failure("failed to update user#{$id}");
        }
        $roles = $request->only(['roles'], []);
        $user->syncRoles($roles);
        return $this->success('update success', UserResource::make($user)->hide(['avatar', 'information']));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        if (empty($id)) {
            return $this->failure('missing parameters');
        }
        $user = $this->model::find($id);
        if (!$user) {
            return $this->failure("failed to get user#{$id}");
        }
        $res = $user->delete();
        if (!$res) {
            return $this->failure("failed to delete user#{$id}");
        }
        return $this->success('delete success');
    }

    public function roles()
    {

    }
}
